// pages/address/modifyNew/modifyNew.js
// pages/address/modify/modify.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示显示    0表示不显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '添加地址', //导航栏 中间的标题
      bg: 'rgba(255, 255, 255, 1)', //导航背景
    },
    buyer_name: '', //收件人姓名
    buyer_mobile: '', //收件人电话
    province_id: '', //收件人省id
    city_id: '', //收件人市id
    area_id: '', //收件人区id
    address: '', //收件人详细地址
    is_default: 0, //是否是默认地址 1是 0 不是
    id: '', //地址id 修改时必传
    addressList: [0, 0, 0], //地址省市区默认下标
    addressSHQ: ['省', '市', '区'],
    regionIndex: [0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    if (options.order == 1) {
      _this.setData({
        order: options.order,
        orderid: options.orderid,
      })
    }else{
      _this.setData({
        pagepath:options.pagepath
      })
    }

    //获取默认省
    $api.getArea({
      login_token: wx.getStorageSync('login_token'),
      level: 1
    }).then((res) => {
      _this.setData({
        addressJson: res.data.data,
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
    //修改地址
    if (options.id) {
      _this.setData({
        loadModal: true
      })
      $api.getUserAddressList({
        login_token: wx.getStorageSync('login_token'),
        address_id: options.id
      }).then((res) => {
        _this.setData({
          loadModal: false,
          id: options.id,
          buyer_name: res.data.data[0].buyer_name,
          buyer_mobile: res.data.data[0].buyer_mobile,
          province_id: res.data.data[0].province_id,
          city_id: res.data.data[0].city_id,
          area_id: res.data.data[0].area_id,
          address: res.data.data[0].address,
          is_default: res.data.data[0].is_default,
          addressSHQ: [res.data.data[0].province_name, res.data.data[0].city_name, res.data.data[0].area_name]
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })

    }
  },
  //地址弹出隐藏
  hideModal(e) {
    this.setData({
      modalName: null,
      addressList: [0, 0, 0]
    })
  },
  //地点底部弹出
  onSHG(e) {
    var _this = this;
    _this.setData({
      // loadModal: true,
      modalName: e.currentTarget.dataset.target,
    })





  },
  //滑动时获取地址index
  onAddressIndex(e) {
    console.log(e)
    var _this = this;
    _this.setData({
      regionIndex: e.detail.value
    })
  },
  //确认地址
  //确认地址
  confirmAddress() {
    var _this = this;
    var regionIndex = _this.data.regionIndex;
    _this.setData({
      ['addressSHQ[0]']: _this.data.addressJson[regionIndex[0]].provinceName,
      province_id: _this.data.addressJson[regionIndex[0]].provinceId,
      ['addressSHQ[1]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityName,
      city_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityId,
      ['addressSHQ[2]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionName,
      area_id: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionId,
      modalName: null,
      regionIndex: [0, 0, 0]
    })
  },
  //获取收件人姓名
  onBuyerName(e) {
    this.setData({
      buyer_name: e.detail.value
    })
  },
  //获取收件人手机
  onBuyerMobile(e) {
    this.setData({
      buyer_mobile: e.detail.value
    })
  },
  //获取收件人详细地址
  onAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //是否是默认地址
  onIsDefault() {
    var _this = this;
    var is_default = _this.data.is_default;
    //1是 0 不是
    if (is_default == 0) {
      _this.setData({
        is_default: 1
      })
    } else {
      _this.setData({
        is_default: 0
      })
    }
  },
  //用户添加 / 修改收货地址
  onEditUserAddress() {
    var _this = this;
    var buyer_name = _this.data.buyer_name;
    var buyer_mobile = _this.data.buyer_mobile;
    var province_id = _this.data.province_id;
    var city_id = _this.data.city_id;
    var area_id = _this.data.area_id;
    var address = _this.data.address;
    var is_default = _this.data.is_default;
    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
    var id = _this.data.id;
    if (buyer_name == '') {
      wx.showToast({
        title: '请添加收货人姓名',
        icon: 'none',
        duration: 2000
      })
    } else if (buyer_mobile == '') {
      wx.showToast({
        title: '请添加手机号码',
        icon: 'none',
        duration: 2000
      })
    } else if (!reg_tel.test(buyer_mobile)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
    } else if (province_id == '') {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 2000
      })
    } else if (address == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        loadModal: true
      })
      if (!id) {
        var addressData = {
          login_token: wx.getStorageSync('login_token'),
          buyer_name: buyer_name,
          buyer_mobile: buyer_mobile,
          province_id: province_id,
          city_id: city_id,
          area_id: area_id,
          address: address,
          is_default: is_default
        }
      } else {
        var addressData = {
          login_token: wx.getStorageSync('login_token'),
          buyer_name: buyer_name,
          buyer_mobile: buyer_mobile,
          province_id: province_id,
          city_id: city_id,
          area_id: area_id,
          address: address,
          is_default: is_default,
          id: id
        }
      }
      $api.editUserAddress(addressData).then((res) => {
        if (res.data.code == 0) {
          if (_this.data.order == 1) {
            _this.setData({
              loadModal: true
            })
            $api.editOrderAddress({
              login_token: wx.getStorageSync('login_token'),
              order_id: _this.data.orderid,
              address_id: res.data.data.address_id,
            }).then((res) => {
              _this.setData({
                loadModal: false
              })
              wx.showToast({
                title: '修改成功',
                icon: 'none',
                duration: 2000
              })
              wx.navigateBack({
                delta: 2,
              })
            }).catch((err) => {
              _this.setData({
                loadModal: false
              })
            })
          } else {
            wx.setStorageSync('addressid', res.data.data.address_id)
            if (!id) {
              _this.setData({
                loadModal: false,
              })
              wx.showToast({
                title: '添加成功',
                icon: 'none',
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: _this.data.pagepath == 'mine'?1:2,
                })
              }, 1500)
            } else {
              _this.setData({
                loadModal: false
              })
              wx.showToast({
                title: '修改成功',
                icon: 'none',
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2,
                })
              }, 1500)
            }
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }




      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })

    }
  },

})